﻿// <copyright file="Collection.cs" company="Microsoft Corporation">
// Copyright (c) 2009 Microsoft Corporation All Rights Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2008-10-04</date>
// <summary>OpenSocial collection</summary>

namespace opensocial
{
    using System;
    using System.Windows.Browser;

    /// <summary>
    /// function delegate to call for each item in the collection
    /// </summary>
    /// <param name="item">the collection item</param>
    public delegate void EachFunction(ScriptObject item);

    /// <summary>
    /// OpenSocial collection
    /// </summary>
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1711:IdentifiersShouldNotHaveIncorrectSuffix", Justification = "This is a collection.")]
    public class Collection
    {
        /// <summary>
        /// Initializes a new instance of the Collection class.
        /// </summary>
        /// <param name="scriptObject">the script object</param>
        public Collection(ScriptObject scriptObject)
        {
            if (scriptObject == null)
            {
                throw new ArgumentNullException("scriptObject");
            }

            this.ScriptObject = scriptObject;
        }

        /// <summary>
        /// Gets or sets the script object
        /// </summary>
        public ScriptObject ScriptObject { get; set; }

        /// <summary>
        /// Get the total size of the collection
        /// </summary>
        /// <returns>the total size of the collection</returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate", Justification = "Mirroring OpenSocial API."), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "get", Justification = "Mirroring OpenSocial API.")]
        public int getTotalSize()
        {
            var result = (double) this.ScriptObject.Invoke("getTotalSize", null);

            return System.Convert.ToInt32(result);
        }

        /// <summary>
        /// Conver the collection to an array
        /// </summary>
        /// <returns>a new Script array</returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "as", Justification = "Mirroring OpenSocial API.")]
        public ScriptArrayCollection asArray()
        {
            var result = ScriptObject.Invoke("asArray") as ScriptObject;

            return new ScriptArrayCollection(result);
        }

        /// <summary>
        /// Applies the function to each of the items in the collection.
        /// </summary>
        /// <param name="function">the function to apply</param>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "each", Justification = "Mirroring OpenSocial API.")]
        public void each(EachFunction function)
        {
            this.ScriptObject.Invoke("each", function);
        }
    }
}
